<template>
  <div class="proDialog">
    <el-dialog
      title="选择商品"
      :visible.sync="visible"
      width="800px"
      z-index="1500"
      append-to-body
      @close="toCancelBox"
    >
      <el-table
        border
        :data="infoList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :row-key="bindRowKeys"
        ref="indexTable"
      >
        <el-table-column
          type="selection"
          align="center"
          :reserve-selection="true"
        >
        </el-table-column>
        <el-table-column
          prop="carouselImgOne"
          label="图片"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <el-image
              v-if="
                scope.row.carouselImgOne != null &&
                scope.row.carouselImgOne != ''
              "
              slot="reference"
              :src="scope.row.carouselImgOne"
              class="image-rows"
              :preview-src-list="[scope.row.carouselImgOne]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="productName"
          label="商品名称"
        >
        </el-table-column>
        <el-table-column
          prop="retailPriceStr"
          label="零售价(元)"
          align="center"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="stockNumber"
          label="商品库存"
          align="center"
          width="100"
        ></el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="pageSizess"
      />
      <div class="determine">
        <el-button
          type="primary"
          @click="determine"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listInfo } from "@/api/product/info";
import { selectOptions } from "@/api/product/category";

export default {
  props: {
    flag: {
      type: Boolean,
    },
  },
  watch: {
    flag(val) {},
  },
  data() {
    return {
      checkedGh: [],
      datas: null,
      selectedDatas: [],
      pageSizess: [5, 10, 15, 20, 25, 30, 50],
      // 商品信息表格数据
      infoList: [],
      // indexTable: [],
      infoListHistory: [],
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        productName: null,
        categoryUid: null,
        brandUid: null,
        minBuyNum: null,
        maxBuyNum: null,
        minCostPirce: null,
        maxCostPirce: null,
        minRetailPrice: null,
        maxRetailPrice: null,
        minTradePrice: null,
        maxTradePrice: null,
        dataStatus: null,
        auditStatus: null,
        stockNumber: null,
        categoryUidOne: null,
        categoryUidTwo: null,
        categoryUidThree: null,
        dataStatus: "1",
      },
      visible: false,
    };
  },
  created() {
    this.visible = this.flag;
    this.getList();
  },
  methods: {
    toCancelBox() {
      this.visible = false;
      this.$emit("toCloseBox", false);
    },
      greet(selectedData) {
        this.selectedDatas = selectedData;
        if(this.selectedDatas != null) this.$refs.indexTable.toggleRowSelection(this.selectedDatas, true);     
      },
    bindRowKeys(row) {
      return row.productUid;
    },
    handleSelectionChange(val) {
      this.checkedGh = val[0];
      if (val.length > 1) {
        this.$refs.indexTable.clearSelection();
        this.$refs.indexTable.toggleRowSelection(val.pop());
      }
      this.datas = this.multipleSelection;
      this.multipleSelection = this.checkedGh;
      if(this.datas) {
        if (this.datas.length > 0) {
        // for (let i = 0; i < this.datas.length; i++) {
        //   for (let k = 0; k < this.multipleSelection.length; k++) {
            if (
              this.datas.productUid == this.multipleSelection.productUid
            ) {
              this.multipleSelection = this.datas;
            }
          }
      }
      //   }
      // }   
    },
    determine() {
      this.$emit("multipleSelection", this.multipleSelection);
      this.infoList.forEach((item) => {
        this.$refs.indexTable.toggleRowSelection(item, false);
      });
      this.$forceUpdate();
      this.open = false;
    },
    /** 查询商品信息列表 */
    getList() {
      if (
        this.queryParams.categoryUid != null &&
        this.queryParams.categoryUid.length > 0
      ) {
        let uidArray = this.queryParams.categoryUid;
        this.queryParams.categoryUidOne = uidArray[0];
        if (uidArray.length > 1) {
          this.queryParams.categoryUidTwo = uidArray[1];
        }
        if (uidArray.length > 2) {
          this.queryParams.categoryUidThree = uidArray[2];
        }
      }
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>
<style scoped>
.determine {
  display: flex;
  justify-content: flex-end;
  color: #fff;
}
</style>
